﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.Authorization;
using AbpZeroTemplate.Authorization;
using AbpZeroTemplate.Organizations;
using AbpZeroTemplate.Organizations.Dto;
using AbpZeroTemplate.Web.Controllers;
using AbpZeroTemplate.Web.Swagger;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace AbpZeroTemplate.Api.Admin.Controllers
{
    /// <summary>获取组织信息
    /// </summary>
    [Area("Admin")]
    [Route("Admin/OrganizationUnit/[action]")]
    [ApiName("Admin")]
    [RemoteService]
    [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits)]
    public class OrganizationUnitController : AbpZeroTemplateControllerBase
    {
        private readonly IOrganizationUnitAppService _organizationUnitAppService;
        /// <summary>
        /// </summary>
        public OrganizationUnitController(IOrganizationUnitAppService organizationUnitAppService)
        {
            _organizationUnitAppService = organizationUnitAppService;
        }

        /// <summary>获取组织信息
        /// </summary>
        [HttpGet]
        public async Task<ListResultDto<OrganizationUnitDto>> GetOrganizationUnits()
        {
            return await _organizationUnitAppService.GetOrganizationUnits();
        }

        /// <summary>获取组织用户
        /// </summary>
        [HttpGet]
        public async Task<PagedResultDto<OrganizationUnitUserListDto>> GetOrganizationUnitUsers(GetOrganizationUnitUsersInput input)
        {
            return await _organizationUnitAppService.GetOrganizationUnitUsers(input);
        }

        /// <summary>创建组织
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree)]
        public async Task<OrganizationUnitDto> CreateOrganizationUnit([FromBody]CreateOrganizationUnitInput input)
        {
            return await _organizationUnitAppService.CreateOrganizationUnit(input);
        }

        /// <summary>更新组织
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree)]
        public async Task<OrganizationUnitDto> UpdateOrganizationUnit([FromBody]UpdateOrganizationUnitInput input)
        {
            return await _organizationUnitAppService.UpdateOrganizationUnit(input);
        }

        /// <summary>转移组织
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree)]
        public async Task<OrganizationUnitDto> MoveOrganizationUnit([FromBody]MoveOrganizationUnitInput input)
        {
            return await _organizationUnitAppService.MoveOrganizationUnit(input);
        }

        /// <summary>删除组织
        /// </summary>
        [HttpDelete]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree)]
        public async Task DeleteOrganizationUnit(EntityDto<long> input)
        {
            await _organizationUnitAppService.DeleteOrganizationUnit(input);
        }

        /// <summary>将用户添加到组织
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers)]
        public async Task AddUsersToOrganizationUnit([FromBody]UsersToOrganizationUnitInput input)
        {
            await _organizationUnitAppService.AddUsersToOrganizationUnit(input);
        }

        /// <summary>将用户移除出组织
        /// </summary>
        [HttpPost]
        [AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers)]
        public async Task RemoveUserFromOrganizationUnit([FromBody]UserToOrganizationUnitInput input)
        {
            await _organizationUnitAppService.RemoveUserFromOrganizationUnit(input);
        }

        [HttpGet]
        [AbpAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers)]
        public async Task<PagedResultDto<NameValueDto>> FindUsers(FindOrganizationUnitUsersInput input)
        {
            return await _organizationUnitAppService.FindUsers(input);
        }


        /// <summary>判断用户是否在组织中
        /// </summary>
        //[HttpPost]
        //[AbpMvcAuthorize(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers)]
        //public async Task<bool> IsInOrganizationUnit([FromBody]UserToOrganizationUnitInput input)
        //{
        //    return await _organizationUnitAppService.IsInOrganizationUnit(input);
        //}
    }
}
